package com.gupt.youhd.service.impl;/*
@author name
@data 2023/6/6-17:00
*/

import com.gupt.youhd.entity.CartItem;
import com.gupt.youhd.entity.Product;
import com.gupt.youhd.entity.User;
import com.gupt.youhd.exception.BusinessException;
import com.gupt.youhd.mapper.CartMapper;
import com.gupt.youhd.service.CartService;
import com.gupt.youhd.util.YouHDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private YouHDUtils youHDUtils;

    @Override
    public List<CartItem> retrieveAllCartItemByUserId(HttpServletRequest request,String username) {

        User user = youHDUtils.getUser(request);

        //根据用户id查询当前用户购物车商品
        List<CartItem> cartItems = cartMapper.retrieveAllCartItemByUserId(user.getUserId());
        return cartItems;
    }

    //删除用户当前购物车商品
    @Override
    public void delete(HttpServletRequest request,int productId) {

        User user = youHDUtils.getUser(request);

        //根据用户id 商品id删除购物车商品
        cartMapper.delCartItemByUserIdProductId(user.getUserId(),productId);
    }


    //添加商品到购物车
    @Override
    public void insert(HttpServletRequest request,Integer productId) {

        User user = youHDUtils.getUser(request);

        Product product = new Product();
        product.setProductId(productId);


        CartItem cartItem = new CartItem();
        cartItem.setUserId(user.getUserId());
        cartItem.setProduct(product);
        cartItem.setJoinTime(new Date());
        cartItem.setChecked(0);

        /**
         * 判断当前购物车有无商品
         * 没有：默认商品数量为1
         * 有：当前数量+1
         */

        CartItem cartItems = cartMapper.retrieveCartItemByUserProId(user.getUserId(),productId);

        if(cartItems == null){
            cartItem.setQuantity(1);
            //添加购物车
            cartMapper.addCartItem(cartItem);
        }else {
            cartItem.setQuantity(cartItems.getQuantity()+1);
            //修改购物车
            cartMapper.updateQuantityByUserProId(cartItem);
        }
    }

    @Override
    public void update(HttpServletRequest request, Integer productId,int status) {

        User user = youHDUtils.getUser(request);

        //status 0:商品数量减一
        //status 1：商品数量加一
        Product product = new Product();
        product.setProductId(productId);
        CartItem cartItem = new CartItem();
        cartItem.setUserId(user.getUserId());
        cartItem.setProduct(product);
        CartItem cartItems = cartMapper.retrieveCartItemByUserProId(user.getUserId(),productId);
        if(status == 0){
            if(cartItems.getQuantity() <= 1){
                throw new BusinessException("最少购买一件哦！");
            }
            cartItem.setQuantity(cartItems.getQuantity()-1);
        }else if(status == 1){
            cartItem.setQuantity(cartItems.getQuantity()+1);
        }
        cartMapper.updateQuantityByUserProId(cartItem);
    }

    /**
     * 批量删除购物车信息
     * @param request
     * @param productId
     */
    @Override
    public void batchDel(HttpServletRequest request, List<String> productId) {

        User user = youHDUtils.getUser(request);

        int count = cartMapper.batchDelCartItemByUserIdProductId(user.getUserId(), productId);
        log.info("批量删除用户信息条数：{}条",count);
    }

}
